package com.qk;

import com.qk.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author 28188
 */
@Slf4j
@Component
@EnableScheduling
public class CachePreheatRunner implements ApplicationRunner {

    @Autowired
    private ReportService reportService;

    /**
     * 缓存预热
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("缓存预热开始");
        reportService.getOverview();
        log.info("缓存预热结束");
    }

    /**
     * 定时刷新缓存
     */
    @Scheduled(cron = "0 */5 * * * ?")
    public void refreshCache() {
        log.info("开始定时刷新缓存...");
        try {
            reportService.refreshOverviewCache();
            log.info("缓存刷新成功！");
        } catch (Exception e) {
            log.error("缓存刷新失败：{}", e.getMessage(), e);
        }
    }
}
